<template>
  <el-form :inline="true" @submit.native.prevent>
    <el-form-item>
      <el-select v-model="queryParam.status" clearable
                 placeholder="使用状态" @change="handleQuery">
        <el-option v-for="item in Dic.partsStatus()"
                   :key="item.value"
                   :label="item.text"
                   :value="item.value"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-select v-model="queryParam.cateId" placeholder="选择分类"
                 filterable clearable
                 @change="handleQuery">
        <el-option v-for="item in cateList"
                   :key="item.pfId"
                   :label="item.pfName"
                   :value="item.pfId"/>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="queryParam.keyword"
                @keyup.enter="handleQuery"
                placeholder="请输入名称"
                type="text"
                prefix-icon="search"
                auto-complete="off"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="openRow">新增</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData.list" v-loading="loading" border height="calc(100vh - 210px)">
    <el-table-column label="配件ID" prop="pfId" width="150" align="center"/>
    <el-table-column label="分类名称" prop="cateName" min-width="90" align="center"/>
    <el-table-column label="配件名称" prop="pname" min-width="260" align="left"/>
    <el-table-column label="配件串号" prop="serialnumber" width="160" align="center"/>
    <el-table-column label="零售价" prop="lsPrice" align="center"/>
    <el-table-column label="会员价" prop="hyPrice" align="center"/>
    <el-table-column label="批发价" prop="pfPrice" align="center"/>
    <el-table-column label="最小库存" prop="count" align="center"/>
    <el-table-column label="当前库存" prop="stockNumber" align="center"/>
    <el-table-column label="状态" prop="status" align="center" :formatter="Dic.partsStatus"/>
    <el-table-column label="操作" width="80" align="center" fixed="right">
      <template v-slot="{row}">
        <el-button link type="success" @click="openRow(row)">
          编辑
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination layout="->,total,sizes,prev,pager,next,jumper"
                 v-model:current-page="queryParam.page"
                 v-model:page-size="queryParam.limit"
                 :total="tableData.total" background
                 @current-change="loadList"
                 @size-change="loadList"/>
  <parts-list-detail v-model="openRowDialog"
                     :cateList="cateList"
                     :info="currentRow"
                     :refresh="loadList"
                     :close="()=>{this.openRowDialog = false}"/>
</template>

<script lang="ts">
import partsApi from "@/api/parts";
import partsListDetail from "@/components/parts/partsListDetail.vue";
import Dic from "@/utils/dic"

export default {
  name: "categoryList",
  components: {partsListDetail},
  data() {
    return {
      Dic,
      queryParam: {
        page: 1,
        limit: 20,
        keyword: "",
        cateId: "",
        status: 1
      },
      tableData: {
        list: [],
        total: 0,
      },
      cateList: [],
      openRowDialog: false,
      currentRow: {},
      loading: false,
    };
  },
  created() {
    this.loadCateList();
    this.loadList();
  },
  methods: {
    handleQuery() {
      this.queryParam.page = 1;
      this.loadList();
    },
    loadList() {
      this.loading = true
      partsApi.partsList(this.queryParam).then(res => {
        this.loading = false
        this.tableData = res.data
      })
    },
    openRow(row) {
      this.openRowDialog = true;
      this.currentRow = row
    },
    loadCateList() {
      partsApi.categoryList({byLan: true, status: 1, limit: 100}).then(res => {
        this.cateList = res.data.list
      })
    },
  }
}
</script>
<style lang="scss" scoped>

</style>